/*
 * Copyright (c) 2022, smartmx <smartmx@qq.com>
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-02-22     smartmx      the first version
 * 2022-03-15     smartmx      each mbtn has it's own max multi-click times
 *
 */

#include <rtthread.h>
#include <board.h>
#include <drivers/pin.h>
#include "drv_pin.h"
#include "mfbd.h"
#include "beep.h"
#include "work_mode_thread.h"

void bsp_btn_value_report(mfbd_btn_code_t btn_value);
unsigned char bsp_btn_check(mfbd_btn_index_t btn_index);

#ifndef TOUCH_BUTTON_KEY0
#define TOUCH_BUTTON_KEY0 GET_PIN(A, 1)
#endif

/* mbtn test */
// 0x1501,//BTN_DOWN_CODE
// 0x1500,//BTN_UP_CODE
// 0,     //BTN_LONG_CODE
// 0x1511,//BTN_REPEAT_1_CODE
// 0x1521,//BTN_REPEAT_2_CODE
// 0x1531,//BTN_REPEAT_3_CODE
// 0x1541,//BTN_REPEAT_4_CODE
// 0x1551,//BTN_REPEAT_5_CODE
// 0x1561,//BTN_REPEAT_6_CODE
// 0x1571,//BTN_REPEAT_7_CODE
// 0x1581,//BTN_REPEAT_8_CODE
// 0x1591,//BTN_REPEAT_9_CODE

/* MFBD_MBTN_DEFINE(NAME, NEXT, BTN_INDEX, FILTER_TIME, REPEAT_TIME, LONG_TIME, MULTICLICK_TIME, MAX_MULTICLICK_STATE, BTN_DOWN_CODE, BTN_UP_CODE, BTN_LONG_CODE, ...) */
MFBD_MBTN_DEFINE(test_mbtn, NULL, 1, 3, 30, 150, 75, 9,
                 0x1501,
                 0x1500,
                 0,
                 0x1511,
                 0x1521,
                 0x1531,
                 0x1541,
                 0x1551,
                 0x1561,
                 0x1571,
                 0x1581,
                 0x1591);

const mfbd_group_t test_mbtn_group =
    {
        bsp_btn_check,
        bsp_btn_value_report,
        &test_mbtn,
};

unsigned char bsp_btn_check(mfbd_btn_index_t btn_index)
{
    switch (btn_index)
    {
    case 1:
        if (rt_pin_read(TOUCH_BUTTON_KEY0) == 1)
        {
            return 1;
        }
        break;
    default:
        break;
    }
    return 0;
}

void bsp_btn_value_report(mfbd_btn_code_t btn_value)
{
    static rt_bool_t start_or_stop_flag = 1;
//    rt_kprintf("%04x\n", btn_value);
    if (btn_value == 0x1500) // BTN_UP_CODE
    {
        if (get_work_mode() == WORK_MODE_POMODORO)
        {
            beep(1, 200, 50, 4000);
            if (start_or_stop_flag == 1)
            {
                start_pomodoro_mode();
                start_or_stop_flag = 0;
            }
            else
            {
                stop_pomodoro_mode();
                start_or_stop_flag = 1;
            }
        }else if (get_work_mode() == WORK_MODE_NUM_COUNTER)
        {
            beep(1, 50, 50, 4000);
            increase_num_counter();
        }
    }
}

static void mfbd_scan(void *arg)
{
    while (1)
    {
        mfbd_mbtn_scan(&test_mbtn_group); /* scan multi-function button group */
        rt_thread_mdelay(10);             /* scan period: 10ms */
    }
}

static void user_button_init(void)
{
    rt_pin_mode(TOUCH_BUTTON_KEY0, PIN_MODE_INPUT_PULLDOWN); /* set KEY pin mode to input */
}

int mfbd_main(void)
{
    rt_thread_t tid = RT_NULL;

    user_button_init();

    /* Create background ticks thread */
    tid = rt_thread_create("mfbd", mfbd_scan, RT_NULL, 1024, 10, 10);
    if (tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }

    return 0;
}

INIT_APP_EXPORT(mfbd_main);
